<?php

require_once BASE_PATH.'application/models/basedao'.EXT;

class moduleDAO extends baseDAO {
	
	var $table = 'modules';
	var $key   = 'id';
	var $sql   = '';

	function __construct()
	{
	    // Call the Model constructor
	    parent::__construct();
           
	}
	
	
	function _defaultSQL()
	{
		$this->db->where(array("published"=>1,"string_id !="=>""));
	}
	
        function findByName($name){
		$this->_defaultSQL();
		
		$this->db->where_in('title',$name);
		$this->db->limit(1);
		$query = $this->db->get($this->table);
		return $query->row();
        }
	
	
	function getParamsByStringId($id)
	{	
		$this->db->limit(1);
		$record = $this->findByColumn("string_id",$id);
		
		if(count($record) == 0)
			return;
		else
			return json_decode($record->params);
	}
	
	
	function getParamsById($id)
	{
		$record = $this->_findById($id);
		
		if(count($record) == 0)
			return;
		else
			return json_decode($record->params);
	}
	
	
	function getResultByStringId($id=1,$data=array())
	{
                $this->load->helper('text');
		$this->load->model("itemDAO");
		$params = $this->getParamsByStringId($id);
		
		
		if(count($params) == 0)
			return;
		
		$this->itemDAO->_defaultSQL(0);
		$records = array();
		$results = array();
		
		if(count($params) > 0)
		{
			//$params = array_map('trim',$params);
			
			
			if(isset($params->image) && $params->image!= "0")
			{
				$this->db->where("image",$params->image);
			}
			
			/*
				Mostramos solo contenido de:
				items,video,audio,gallery � cartoon
			*/
			if(isset($params->type) && $params->type!= "0")
			{
				switch($params->type)
				{
					case "item":
						$this->db->where(array('fulltext !='=>''));	
					break;
					case "video":
						$this->db->where(array('video !='=>''));	
					break;
					case "audio":
						$this->db->where(array('audio !='=>''));	
					break;
					case "gallery":
						$this->db->where(array('gallery !='=>''));	
					break;
					case "cartoon":
						$this->db->where(array('fulltext'=>'','video'=>'','audio'=>'','gallery'=>''));
					break;
				}
			}

			/*
				Mostrar unicamente articulos de acuerdo al usuario creador
			*/
			if(isset($params->userid) && !empty($params->userid))
				$this->db->where_in("created_by",$params->userid);

			/*
				Mostrar unicamente articulos de acuerdo al grupo del usuario
			*/
			if(isset($params->created_group) && !empty($params->created_group))
				$this->db->where_in("created_group",$params->created_group);
				
				
			
			/*
			colocamos el ordenamiento
			*/
			if($params->order_by != "0" and $params->id == "0")
			{
				
				
				if($params->order_by =="random")
				{
					$params->orientation = $params->order_by;
					$params->order_by = $this->itemDAO->key;	
				}
				
				$this->db->order_by($params->order_by,$params->orientation);
			}

			/*
			Colocamos las categorias mientras NO este seteado los "IDS de Art?culos".
			*/
			
			if($params->id == "0" and isset($params->catid))
				$this->db->where_in("catid",$params->catid);
			
			/*
			Colocamos los IDS mientras SI este seteado los "IDS de Art?culos".
			*/
			
			if(isset($params->id)  and $params->id != "0")
			{	
				$valores=explode(',',$params->id);
				foreach($valores as $itemss){
					if($itemss!="")
					$nuevosvalores[]=$itemss;
				}
				if(isset($nuevosvalores)){
					$searchids=implode(',',$nuevosvalores);
				}else{
					$searchids="";
				}
				$this->db->where_in("id",explode(',',$searchids));
				$this->db->order_by("FIELD(id,".rtrim($searchids, ",")." )");
			}
			
			/*
			Colocamos el Limite de items
			*/
			
			if($params->limit != "0")
				$this->db->limit($params->limit);
			else
				$this->db->limit(PAGINACION);
			
			/*
			Colocamos el limite por DIAS
			*/
			if($params->last_days != "0") 
				$this->db->where('created >= DATE_ADD(NOW(),INTERVAL -'.$params->last_days.' DAY)', NULL, FALSE);
			

			$this->_sql($data);
			$query = $this->db->get($this->itemDAO->table); 
			$result = $query->result();
			
			
			
			if($params->limit_introtext != "0" || $params->limit_fulltext != "0" || $params->limit_title != "0")
			{
				
				$results = array();
				foreach($result as $record)
				{
					
					
					if($params->limit_title != "0")
						$record->title = word_limiter($record->title, $params->limit_title);
                                                
					if($params->limit_introtext != "0")	
						$record->introtext = word_limiter(strip_tags($record->introtext), $params->limit_introtext);
						
					if($params->limit_fulltext != "0")	
						$record->fulltext = word_limiter(strip_tags($record->fulltext), $params->limit_fulltext);
                                        
					
					$results[] = $record;	
				}	
			}else
				$results = $result;
			
			

		}
		
		/* agregamos las imagenes de las galerias */
		
		if(isset($params->type) && $params->type == "gallery")
		{
			
			$this->load->model("gallerydao");
			
			foreach($results as $record)
			{
				$gallery = $this->gallerydao->getImagesById($record->id);
				$records[] =(Object) array_merge((array)$record,array("gallery"=>$gallery));
			}
			
			$results = $records;
		}
		
		
		
		//$results = array_map('trim',$results);
		
		return $results;
	}
	
}

?>